package com.westa.smallshop.goodsdetail;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseDetailFragment;
import com.westa.smallshop.common.BasePresenter;
import com.westa.smallshop.common.GetPresenterInterface;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.util.HttpUtils;
import com.westa.smallshop.util.UserSPUtils;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy123 on 2016/9/14.
 */
public class GoodsDetailFragment extends BaseDetailFragment<GoodsDetailContract.Presenter> implements GoodsDetailContract.View, GetPresenterInterface {


    @BindString(R.string.title_goods_detail)
    String mTitle;
    @BindView(R.id.iv_goods_image_show)
    ImageView mIvGoodsImageShow;
    @BindView(R.id.tv_goods_name)
    TextView mTvGoodsName;
    @BindView(R.id.tv_goods_price)
    TextView mTvGoodsPrice;
    @BindView(R.id.tv_goods_desc)
    TextView mTvGoodsDesc;
    @BindView(R.id.tv_goods_detail_cart_price)
    TextView mTvGoodsDetailCartPrice;
    @BindView(R.id.btn_cart_confirm)
    Button mBtnCartConfirm;
    @BindView(R.id.iv_shop_goods_item_sold)
    ImageView mIvShopGoodsItemSold;

    @NonNull
    private GoodsDetailContract.Presenter mPresenter;
    private GoodsItem mGoodsItem;

    public GoodsDetailFragment() {
        // Required empty public constructor
    }

    public static GoodsDetailFragment newInstance() {

        return new GoodsDetailFragment();
    }

    @Override
    public void init() {

        getActivity().setTitle(mTitle);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_goods_detail;
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void failedOperation(String errorMsg) {

    }

    @Override
    public void refreshData() {

        mPresenter.requestCard();
    }

    @Override
    public void showGoodsInfo(GoodsItem goodsItem) {

        mGoodsItem = goodsItem;
        Glide.with(this)
                .load(HttpUtils.getHost() + goodsItem.getGoods_pic())
                .centerCrop()
                .into(mIvGoodsImageShow);
        mTvGoodsName.setText(goodsItem.getGoods_name());
        mTvGoodsDesc.setText(goodsItem.getGoods_desc());
        mTvGoodsPrice.setText(getString(R.string.price_format, String.valueOf(goodsItem.getGoods_price())));
        mTvGoodsDetailCartPrice.setText(getString(R.string.price_format, String.valueOf(goodsItem.getGoods_price())));
        if (!goodsItem.isState()) {
            mIvShopGoodsItemSold.setVisibility(View.VISIBLE);
            mBtnCartConfirm.setEnabled(false);
        }
        if (TextUtils.equals(goodsItem.getAccount_id(),UserSPUtils.getAccount(getContext()))){
            mBtnCartConfirm.setEnabled(false);
        }
    }

    @Override
    public void showAddToCartSuccessful() {
        showSingleToast(R.string.tst_add_to_cart_successful);
    }

    @Override
    public void showAddToCartFailed() {
        showSingleToast(R.string.tst_add_to_cart_failed);
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void setPresenter(@NonNull GoodsDetailContract.Presenter presenter) {

        mPresenter = checkNotNull(presenter);
    }

    @OnClick(R.id.btn_cart_confirm)
    public void onClick() {
        mPresenter.addGoodsToCart(mGoodsItem.getGoods_id());
    }

}
